package com.gxuwz.attendance.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.gxuwz.attendance.entity.Attendance;
import com.gxuwz.attendance.entity.Record;
import com.gxuwz.attendance.service.AttendanceService;
import com.gxuwz.attendance.service.ClassService;
import com.gxuwz.attendance.util.JsonUtil;
import com.gxuwz.attendance.vo.Page;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import net.sf.json.JSONArray;

public class AttendanceAction extends BaseAction implements ModelDriven<Attendance>,Preparable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static String TEACHER_MANAGE = "/page/attendance/attendance_manage.jsp";
	private static String TEACHER_RECORD = "/page/attendance/attendance_list.jsp";
	private Attendance attendance;
	private Record record;
	@Autowired
	private AttendanceService service;
	@Autowired
	private ClassService classService;
	private String rows;
	private String page;
	private String type;
	private String num = null;
	private String data;
	private String ids;
	private Map<String, Object> map = new HashMap<String, Object>();
	
	
	
	/**
	 * 跳转到课堂考勤页面
	 * @return
	 */
	public String index() {
		classService.findByName(null);
		forwardView = TEACHER_MANAGE;
		
		return SUCCESS;
	}	
	
	/**
	 * 返回考勤结果页面
	 * @return
	 */
	public String record() {
		classService.findByName(null);
		forwardView = TEACHER_RECORD;
		
		return SUCCESS;
	}
	
	
	
	/**
	 * 课堂考勤（随机考勤和全部点名）
	 */
	public void list() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();

		// 判断随机考勤人数
		if(num != null && !"".equals(num)) {
			map.put("total", Integer.parseInt(num));
			add();// 随机考勤
		}else {
			// 全部点名
			map.put("rows", service.findByPage(attendance, new Page(Integer.parseInt(page), Integer.parseInt(rows))));
			map.put("total", session.getAttribute("size"));
			
			JsonUtil.toJons(ServletActionContext.getResponse(), map);
		}
		
	}
	
	/**
	 * 添加考勤
	 */
	public void add() {
		if(num == null || "".equals(num)) {
			return;
		}
		List<Attendance> list = service.findByNum(attendance, num);
		map.put("rows", list);
		if(list.isEmpty()) {
			map.put("type", "error");
			map.put("msg", "没有符合条件信息");
		}else {
			map.put("type", "success");
			map.put("msg", "添加成功");
		}
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
	}
	
	/**
	 * 保存考勤记录
	 */
	public void save() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
	
		boolean flag = service.batchSave(data);
		if(flag == true) {
			map.put("type", "success");
			map.put("msg", "保存成功");
			
			session.setAttribute("result", data);
		}else {
			map.put("type", "error");
			map.put("msg", "保存失败！请重新保存");
		}
		
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
	}
	
	/**
	 * 当前考勤结果
	 */
	public void currentRecord() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();

		JSONArray array = JSONArray.fromObject(session.getAttribute("result"));
		
		map.put("rows", JSONArray.fromObject(session.getAttribute("result")));
		map.put("total", array.size());
		
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
	}
	

	@Override
	public void prepare() throws Exception {
		if(attendance == null) {
			attendance = new Attendance();
		}
		if(record == null) {
			record = new Record();
		}
	}

	@Override
	public Attendance getModel() {
		return attendance;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Record getRecord() {
		return record;
	}

	public void setRecord(Record record) {
		this.record = record;
	}
	

}
